<?php  
if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * 
 * 后台管理员角色权限管理模型类
 * @author 66diqiu-li
 *
 */
class Admin_Adminroleauthors_model extends CI_Model {

    /**
     * @var 表名称
     */
    private $table;
    private $sid;

    public function __construct()
    {
        parent::__construct();
        $this->sid = $GLOBALS['sid'];
        $this->table  = $this->db->dbprefix("admin_role_authors");
    }

    /**
     *
     * 添加多条数据
     * @param int $role_id 角色ID
     * @param $data
     */
    function add_batch($role_id,$data){

        //删除旧的数据
        $where = $this->set_where(array('role_id'=>$role_id));
        $this->db->where($where);
        $this->db->delete($this->table);

        //添加信息的数据
        return $this->db->insert_batch($this->table,$data);
    }
	
    /**
     * 获取单条记录
     * @param array $where 查询条件
     * @param string $field 查询字段
     * @return mixed
     */
    function get_row($where=array(),$field='*'){
        $where = $this->set_where($where);
        if(!empty($field))$this->db->select($field);
        $query = $this->db->get_where($this->table,$where,1);
        return $query->row_array();
    }
	
    /**
     *
     * 删除数据
     * @param array $where 删除条件
     */
    function remove_row($where=array()){
        $where = $this->set_where($where);
        if(!empty($where))$this->db->where($where, NULL, FALSE);
        return $this->db->delete($this->table);
    }

    /**
     * 获取所有管理管理员角色权限
     * @param $where 选择条件
     * @return array
     */
    function get_list($where){
        $where = $this->set_where($where);
        $this->db->select('role_id,app,controller,action');
        $query = $this->db->get_where($this->table,$where);
        $data = array();
        foreach($query->result_array() as $row){
            $data[] = $row;
        }
        return $data;
    }
	
    /**
     *
     * 获取总数量
     * @param array $where 查询条件
     */
    function get_count($where=array()){
        $where = $this->set_where($where);
        if(!empty($where))$this->db->where($where, NULL, FALSE);
        $this->db->select('role_id');
        $this->db->from($this->table);
        return $this->db->count_all_results();
    }

    /**
     * 检查指定菜单是否有权限
     * @param $data  表中数组，单条
     * @param $roleid 需要检查的角色ID
     * @param array $priv_data 已授权权限表数据
     * @return bool
     */
    public function isCompetence($data, $roleid, $priv_data = array()) {
        $priv_arr = array('app', 'controller', 'action');
        if ($data['app'] == '') {
            return false;
        }
        if (empty($priv_data)) {
            //查询已授权权限
            $priv_data = $this->get_list(array('role_id'=>$roleid));
        }
        
        if (empty($priv_data)) {
            return false;
        }
        //菜单id
        $menuid = $data['id'];
        
        //菜单类型
        $type = $data['type'];
        
        //去除不要的数据
        foreach ($data as $key => $value) {
            if (!in_array($key, $priv_arr)) {
                unset($data[$key]);
            }
        }
        $competence = array(
            'role_id' => $roleid,
            'app' => $data['app'],
        );
        //如果是菜单项加上菜单Id用以区分，保持唯一
//        if ($type == 0) {
//            $competence["controller"] = $data['controller'] . $menuid;
//            $competence["action"] = $data['action'] . $menuid;
//        } else {
            $competence["controller"] = $data['controller'];
            $competence["action"] = $data['action'];
//        }
        //检查是否在已授权列表中
        $implode = implode('', $competence);
        $info = in_array(implode('', $competence), $this->privArrStr($priv_data));
        if ($info) {
            return true;
        } else {
            return false;
        }
    }
    
    /**
     * 按规则排序组合
     * @param type $priv_data
     * @return array
     */
    private function privArrStr($priv_data) {
        $privArrStr = array();
        if (empty($priv_data)) {
            return $privArrStr;
        }
        foreach ($priv_data as $rs) {
            $competence = array(
                'role_id' => $rs['role_id'],
                'app' => $rs['app'],
                'controller' => $rs['controller'],
                'action' => $rs['action'],
            );
            $privArrStr[] = implode('', $competence);
        }
        return $privArrStr;
    }

    /**
     * 返回查询条件
     * @param $where 查询条件
     * @return array|string
     */
    function set_where($where){
        if(is_array($where))$where['sid']=$this->sid;
        else $where=$where?$where.' AND sid='.$this->sid:'sid='.$this->sid;
        return $where;
    }
}